package cn.px.rest.controller.check;

import cn.px.common.util.R;
import cn.px.dao.dto.check.CheckPlanDTO;
import cn.px.dao.entity.check.CheckPlan;
import cn.px.service.check.CheckPlanService;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.check.ExportCheckPlanVO;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.rest.log.annotation.SysLog;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 点检计划 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/check/plans")
public class CheckPlanController {

  private final  CheckPlanService checkPlanService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<CheckPlan> getById(@PathVariable("id") String id){
      return new R<>(checkPlanService.getModelById(id));
  }

  /**
   * 新增记录
   *
   * @param checkPlanDTO 点检计划DTO
   * @return R
   */
  @SysLog("新增点检计划")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('check-spot-plans-add') or @pms.hasPermission('check-polling-plans-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody CheckPlanDTO checkPlanDTO) {
      return new R<>(checkPlanService.saveModelByDTO(checkPlanDTO));
  }

  /**
   * 修改记录
   *
   * @param checkPlanDTO 点检计划DTO
   * @return R
   */
  @SysLog("修改点检计划")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('check-spot-plans-edit') or @pms.hasPermission('check-polling-plans-add')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody CheckPlanDTO checkPlanDTO) {
      checkPlanService.modModelByDTO(checkPlanDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除点检计划")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('check-spot-plans-del') or @pms.hasPermission('check-polling-plans-add')")
  public R removeById(@PathVariable String id){
    checkPlanService.deleteByPrimaryKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除点检计划")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('check-spot-plans-del') or @pms.hasPermission('check-polling-plans-add')")
    public R removeIds(@RequestBody List<String> ids){
        checkPlanService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param checkPlanDTO 点检计划DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<CheckPlan>> query(CheckPlanDTO checkPlanDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(checkPlanService.selectPageInfo(checkPlanDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param checkPlanDTO 点检计划DTO
   * @return R
   */
  @GetMapping("")
  public R query(CheckPlanDTO checkPlanDTO) {
      return new R<>(checkPlanService.getModelListByDTO(checkPlanDTO));
  }

  /**
     * 点检计划导出
     *
     * @param checkPlanDTO 点检计划DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("点检计划导出")
  @PreAuthorize("@pms.hasPermission('check-spot-plans-export') or @pms.hasPermission('check-polling-plans-add')")
  public void export(HttpServletResponse response, CheckPlanDTO checkPlanDTO) {
    List<CheckPlan> list = checkPlanService.getModelListByDTO(checkPlanDTO);
    ExcelUtil.exportResponseDict(response, ExportCheckPlanVO.class, BeanConverterUtil.copyListProperties(list, ExportCheckPlanVO.class), "点检计划");
  }

}
